<template>
	<!-- 个人资料 -->
	<view class="container">
		<view class="box">
			<view class="item" @tap="uploadImg">
				<view class="title">
					头像
				</view>
				<image :src="avatar" mode=""></image>
			</view>
			<view class="item">
				<view class="title">
					昵称
				</view>
				<input type="text" v-model="driver.nickname" class="text" placeholder="请输入昵称"/>
			</view>
			<view class="item">
				<view class="title">
					性别
				</view>
				<view class="text"  @change="bindPickerChange">
					<picker mode="selector" :range="arr" @change="bindPickerChange">
						<view v-if="index!=-1">{{arr[index]}}</view>
						<view v-if="index==-1">{{arr[driver.gender]}}</view>
					</picker>
				</view>
			</view>
			<view class="item">
				<view class="title">
					联系电话
				</view>
				<input type="text" disabled="ture" v-model="driver.phone" class="text" placeholder="请输入联系电话"/>
			</view>
		</view>
		<view class="botBtn" @tap="tijiao">
			保存
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				arr:['男','女'],
				index:-1,
				avatar:'',
				driver:'',
			};
		},
		onLoad(e){
			this.token=e.token
			uni.getStorage({
			    key: 'user',
			    success: (res)=> {
					this.driver=res.data
					// console.log(res)
					this.avatar=this.webUrl+res.data.avatar
			    },
			});
		},
		methods:{
			goChangePhone(){
				uni.navigateTo({
					url:'changePhone/changePhone?token='+this.token
				})
			},
			tijiao(){
				uni.showLoading({
					title: '加载中',
					mask: false
				});
				// console.log(this.driver.avatar)
				uni.request({
					url: this.webUrl+'/user/my/profile',
					method: 'PUT',
					header:{
						token:this.token
					},
					data: {
						avatar:this.driver.avatar,
						nickname:this.driver.nickname,
						gender:this.driver.gender
					},
					success: res => {
						console.log(res)
						uni.hideLoading()
						if(res.data.code==0){
							uni.setStorage({
								key: 'user',
								data: res.data.data,
								success: function() {
								}
							});
							uni.showToast({
								title: '保存成功',
								icon:'success',
								duration:2000
							});
							setTimeout(()=>{
								uni.navigateBack({
									delta: 1
								});
							},2000)
						}else if(res.data.code==3){
							uni.showToast({
								title: res.data.msg,
								icon:'none',
								duration:2000
							});
						}else{
							uni.showToast({
								title: '网络错误',
								icon:'none',
								duration:2000
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '网络错误',
							icon:'none',
							duration:2000
						});
					},
					complete: () => {}
				});
			},
			bindPickerChange: function(e) {
				// console.log('picker发送选择改变，携带值为', e.target.value)
				this.index = e.target.value
				this.driver.gender=this.index
			},
			uploadImg(){
				uni.chooseImage({
				    success: (chooseImageRes) => {
						uni.showLoading({
							title: '上传中',
							mask: false
						});
				        const tempFilePaths = chooseImageRes.tempFilePaths;
						this.avatar=tempFilePaths[0]
				        uni.uploadFile({
				            url: this.webUrl+'/index/index/upload', //仅为示例，非真实的接口地址
				            filePath: tempFilePaths[0],
				            name: 'file',
				            formData: {
				                'user': 'test'
				            },
				            success: (uploadFileRes) => {
								this.driver.avatar=JSON.parse(uploadFileRes.data).data
								uni.showToast({
									title: '上传成功',
									icon:'success',
									duration:2000
								});
								// console.log(this.driver.avatar)
				            }
				        });
				    }
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container{
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: #F2F2F2;
		overflow: hidden;
		.box{
			background:rgba(255,255,255,1);
			border-radius:10px;
			margin-top: 30upx;
			.item{
				display: flex;
				margin: 0 30upx;
				justify-content: space-between;
				height: 90upx;
				line-height: 90upx;
				box-sizing: border-box;
				border-bottom: 2upx solid #EDEDED;
				.title,.text{
					font-size:30upx;
					font-family:Microsoft YaHei;
					font-weight:400;
					color:rgba(51,51,51,1);
					picker{
						width: 500upx;
						text-align: right;
					}
				}
				image{
					width: 50upx;
					height: 50upx;
					margin-top: 20upx;
					border-radius: 50%;
				}
				.text{
					padding-left: 100upx;
				}
				input{
					margin-top: 24upx;
					text-align: right;
				}
			}
		}
		.botBtn{
			width: 200upx;
			height: 90upx;
			color: #FFFFFF;
			background: #fe5621;
			margin: 400upx auto 0;
			border-radius: 10upx;
			text-align: center;
			line-height: 90upx;
		}
	}
</style>
